package shared.client.io;

import java.util.HashMap;

/**
 * This class enumerates the various commands available to the client.
 * 
 * @author Vincent Tsuei
 * @version 2012-11-14
 */
public enum ClientCommand {
    
    HELP("help", "Shows this menu", 0),
    
    LOGIN("login", "Syntax: login [username] | Log in as [username]", 1),
    BYE("bye", "Quits the client.", 0),
    
    LIST_GAMES("games", "Shows a list of ongoing games with their game IDs", 0),
    LIST_PLAYERS("who", "Shows a list of logged in players", 0),
    
    START_GAME("play", "Syntax: play [otherUserName] | Start a game with [otherUserName]", 1),
    REMOVE("remove", "Syntax: remove [numberTokens] [pile] | Remove [numberTokens] tokens from pile number [pile]. Only works when in game.", 2),
    
    OBSERVE("observe", "Syntax: observe [gameID] | Start observing the game with [gameID]", 1),
    UNOBSERVE("unobserve", "Syntax: unobserve [gameID] | Stop observing the game with [gameID]", 1);
    
    
    private static String cache = null;
    private static final HashMap<String, ClientCommand> stringToCommand = new HashMap<String, ClientCommand>();
    
    static {
        for (ClientCommand c : ClientCommand.values()) {
            stringToCommand.put(c.commandString.toLowerCase(), c);
        }
    }
    
    private String commandString, description;
    private final int argumentNumber;
    
    
    ClientCommand(String command, String desc, int argNumber) {
        commandString = command;
        description = desc;
        argumentNumber = argNumber;
    }
    
    public boolean validNumberOfArgs(int numberOfArgs) {
        return argumentNumber == numberOfArgs;
    }
    
    public String getCommandString() {
        return commandString;
    }
    
    public String getCommandDescription() {
        return description;
    }
    
    @Override
    public String toString() {
        return commandString;
    }
    
    
//    private static String cache2;
//    public static String getGameMenuString() {
//        if (cache2 == null) {
//            String nl = System.getProperty("line.separator");
//            StringBuilder sb = new StringBuilder("============== Menu ==============");
//            sb.append(nl);
//            for (ClientCommand c : ClientCommand.values()) {
//                
//                sb.append("[").append(c.ordinal() + 1).append("] ");
//                sb.append(c.getCommandString());
//                sb.append(" - ");
//                sb.append(c.getCommandDescription());
//                if (c.ordinal() != ClientCommand.values().length - 1) {
//                    sb.append(nl);
//                }
//            }
//            cache = sb.toString();
//        }
//        return cache2;
//    }
    
    public static String getMenuString() {
        if (cache == null) {
            String nl = System.getProperty("line.separator");
            StringBuilder sb = new StringBuilder("============== Menu ==============");
            sb.append(nl);
            for (ClientCommand c : ClientCommand.values()) {
                sb.append("[").append(c.ordinal() + 1).append("] ");
                sb.append(c.getCommandString());
                sb.append(" - ");
                sb.append(c.getCommandDescription());
                if (c.ordinal() != ClientCommand.values().length - 1) {
                    sb.append(nl);
                }
            }
            cache = sb.toString();
        }
        return cache;
    }
    
    public static ClientCommand getCommandByString(String s) {
        return stringToCommand.get(s.toLowerCase());
    }
}